package com.java1234.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.java1234.dao.IUserDao;
import com.java1234.model.Borrower;
import com.java1234.model.Librarian;
import com.java1234.model.SystemAdministrator;
import com.java1234.model.User;
import com.java1234.util.StringUtil;

public class UserImpl implements IUserDao {
	/**
	 * 登录验证
	 * 
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con, User user) throws Exception {
		User resultOperator = null;
		String sql = "select * from t_user where userType=? and userAccount=? and password=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getUserType());
		pstmt.setString(2, user.getUserAccount());
		pstmt.setString(3, user.getPassword());
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			resultOperator = new User();
			resultOperator.setUserType(rs.getString("userType"));
			resultOperator.setUserAccount(rs.getString("userAccount"));
			resultOperator.setPassword(rs.getString("password"));
		}
		con.close();
		return resultOperator;
	}

	public ResultSet list(Connection con, User user) throws Exception {
		StringBuffer sb = new StringBuffer("select * from t_user");
		if (StringUtil.isNotEmpty(user.getUserType())) {
			sb.append(" and userType like '%" + user.getUserType() + "%'");
		}
		if (StringUtil.isNotEmpty(user.getUserAccount())) {
			sb.append(" and userAccount like '%" + user.getUserAccount() + "%'");
		}
		if (StringUtil.isNotEmpty(user.getPassword())) {
			sb.append(" and password like '%" + user.getPassword() + "%'");
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet i = pstmt.executeQuery();
		return i;
	}

	/**
	 * 数据库user表借阅者信息添加
	 */
	public int addBorrower(Connection con, Borrower borrower) throws Exception {
		String sql = "insert into t_user values(?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, "借阅者");
		pstmt.setString(2, borrower.getBorrowerAccount());
		pstmt.setString(3, borrower.getPassword());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表借阅者信息修改
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int updateBorrower(Connection con, Borrower borrower) throws Exception {
		String sql = "update t_user set password=? where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, borrower.getPassword());
		pstmt.setString(2, borrower.getBorrowerAccount());

		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表借阅者信息删除
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int deleteBorrower(Connection con, String borrowerAccount) throws Exception {
		String sql = "delete from t_user where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, borrowerAccount);
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息添加
	 */
	public int addLibrarian(Connection con, Librarian librarian) throws Exception {
		String sql = "insert into t_user values(?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, "图书管理员");
		pstmt.setString(2, librarian.getLibrarianAccount());
		pstmt.setString(3, librarian.getPassword());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息修改
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int updateLibrarian(Connection con, Librarian librarian) throws Exception {
		String sql = "update t_user set password=? where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, librarian.getPassword());
		pstmt.setString(2, librarian.getLibrarianAccount());

		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息删除
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int deleteLibrarian(Connection con, String librarianAccount) throws Exception {
		String sql = "delete from t_user where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, librarianAccount);
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息添加（未完成）
	 */
	public int addSystemAdministrator(Connection con, SystemAdministrator systemAdministrator) throws Exception {
		String sql = "insert into t_user values(?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, "系统管理员");
		pstmt.setString(2, systemAdministrator.getSystemAdministratorAccount());
		pstmt.setString(3, systemAdministrator.getPassword());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息修改
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int updateSystemAdministrator(Connection con, SystemAdministrator systemAdministrator) throws Exception {
		String sql = "update t_user set password=? where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, systemAdministrator.getPassword());
		pstmt.setString(2, systemAdministrator.getSystemAdministratorAccount());

		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库user表图书管理员信息删除
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int deleteSystemAdministrator(Connection con, String systemAdministratorAccount) throws Exception {
		String sql = "delete from t_user where userAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, systemAdministratorAccount);
		int i = pstmt.executeUpdate();

		return i;
	}
}
